/*
 * @Author: Yao
 * @LastEditors  : ShiJunJie
 * @Date: 2022-04-07 17:24:56
 * @LastEditTime : 2022-05-09 11:15:13
 * @Description: Modify here please
 */
import type { ConfigEnv, UserConfig } from 'vite'
import { loadEnv } from 'vite'
import createViteDefine from './define'
import createViteBuild from './build'
import createViteServer from './server'
import createVitePlugins from './plugins'
import { formatEnv } from './server/utils'
import fs from 'fs'

export async function bootstrapConfig(config: ConfigEnv): Promise<UserConfig> {
  const { command, mode } = config
  const isBuild = command === 'build'
  const envs = formatEnv(loadEnv(mode, `./config/`))
  const options: viteUserOptions = { envs, isBuild, command, mode }

  // vite 基础配置
  const viteConfig: UserConfig = {
    clearScreen: false,
    mode: envs.NODE_ENV == 'pro' ? 'production' : 'development',
    base: envs.VITE_APP_BASE_URL || '/',
    resolve: { dedupe: ['vue'] },
    css: {
      preprocessorOptions: {
        scss: {
          charset: false
        },
        less: {
          charset: false,
          javascriptEnabled: true,
        },
      }
    }
  }

  // if (fs.existsSync('./src/assets/style/global/config.less')) {
  //   viteConfig.css.preprocessorOptions.less.additionalData = (source: string, loaderContext: string) => {
  //     // All less files ending with imports.less
  //     // will not re-import additionalData
  //     const fileName = loaderContext.split('/')
  //     // console.log('less', source.includes('config.less'), fileName[fileName.length - 1], '\n', source);
  //     if (source.includes('config.less') || loaderContext.includes('config.less')) return source;
  //     // Use additionalData from legacy nuxt less options
  //     return `@import "./src/assets/style/global/config.less";
  //     ${source}`
  //   }
  //   // viteConfig.css.preprocessorOptions.less.additionalData = '@import "./src/assets/style/global/config.less";'
  // }
  if (fs.existsSync('./src/assets/style/global/config.scss')) {
    viteConfig.css.preprocessorOptions.scss.additionalData = (source: string, loaderContext: string) => {
      // All scss files ending with imports.scss
      // will not re-import additionalData
      const fileName = loaderContext.split('/')
      // console.log('scss', source.includes('config.scss'), fileName[fileName.length - 1], '\n', source);
      if (source.includes('config.scss') || loaderContext.includes('config.scss')) return source;
      // Use additionalData from legacy nuxt scss options
      return `@import "./src/assets/style/global/config.scss";
      ${source}`
    }
    // viteConfig.css.preprocessorOptions.scss.additionalData = '@import "./src/assets/style/global/config.scss";'
  }

  // 全局变量配置
  viteConfig.define = createViteDefine(options)

  // 打包配置
  viteConfig.build = createViteBuild(options)

  // 开发服务容器环境配置
  viteConfig.server = createViteServer(options)

  // 异步导入项目所需的所有vite插件配置
  viteConfig.plugins = await createVitePlugins(options)

  return viteConfig
}

export { bootstrapConfig as default }
